const app = getApp();
import Api from '@/services/api.js'
export default {
  data() {
    return {
      department: {}, // 部门信息
      allJobs: [], // 所有的岗位,
      adminDepartmentJobIds: ['1730121347229MnHB', '1730121314664J3BI'], // 管理部门公共池里面的部门岗位id
      addedJobs: {}
    }
  },
  computed: {
    privacyData() {
      return this.$store.state.privacyData;
    },
    // 是否是管理部门
    adminDepartment() {
      return this.department.name == '管理岗位';
    },
    // 是否是BOSS
    // isBoss() {
    //   return this.privacyData.roles.includes('boss')
    // }
  },
  onLoad(options) {
    const department = JSON.parse(options.department);
    this.addedJobs = department.children;
    this.department = department;
  },
  onShow() {
    this.getAllJobs();
  },
  methods: {
    async getAllJobs() {
      const { code, result } = await this.$request({
        url: Api.getAllJobs,
      })
      if (code === 200) {
        /**
         * 如果是boss 并且是管理部门 只显示中层管理和高层职业经理人
         * 因为管理部门不是一个部门 不会出现在部门设置与管理里面
         * 所以此处直接把中层管理和高层职业经理人过滤出来
         */
        //  !this.adminDepartment ? 
        let arr = result.filter(i => !this.adminDepartmentJobIds.includes(i.id));
        this.allJobs = arr.map(i => {
          const children = i.children || [];
          // 第二层遍历在判断已添加的岗位数组里面就不在未添加的数组里面显示了
          i.children = children.reduce((p, child) => {
            let addedIndex = -1;
            this.addedJobs.forEach((addedJob, addedIdx) => {
              // 判断是已添加岗位里面是否在该部门里面有对应的岗位
              if (addedJob.id == child.id) {
                /**
                 * 如果有 把对应岗位的父级id 赋值到已添加的岗位数组里面
                 * 解决已添加岗位里面没有公共池pid的问题
                 */
                addedJob.pid = i.id;
                // 用于记录已添加岗位里面是否在该部门里面有对应的岗位  有就不在公共池里面添加了
                addedIndex = addedIdx;
              }
            });
            // const addedIdx = this.addedJobs.findIndex(addedItem => addedItem.id == child.id);
            if (addedIndex == -1) {
              p.push(child)
            }
            return p
          }, [])
          return i;
        });
      }
    },
    handleAccountMgmt() {
      uni.navigateTo({
        url: `/enterpriseMgmt/staffList/index?department=${JSON.stringify(this.department)}`
      })
    },
    // 移除已添加岗位
    handleDelete(e) {
      const { index, item } = e.currentTarget.dataset;
      this.addedJobs.splice(index, 1);
      this.allJobs.map((i, idx) => {
        if (item.pid === i.id) {
          i.children.unshift(item)
        }
      })
    },
    // 未添加岗位添加到已添加岗位
    handleAdd(e) {
      const { parentIndex, childItem, childIndex } = e.currentTarget.dataset;
      this.allJobs[parentIndex].children.splice(childIndex, 1)
      this.addedJobs.push(childItem)
    },
    // 按钮保存事件
    async handleSave() {
      if (!this.addedJobs.length) {
        return uni.showToast({ title: '已添加岗位不能为空', icon: 'none' });
      }
      const jobIds = this.addedJobs.map(i => (i.id)).join(',');
      const { code, result } = await this.$request({
        url: Api.updateJobByDepartmentId,
        method: 'POST',
        data: {
          departmentId: this.department.id,
          jobIds
        }
      })
      if (code === 200) {
        uni.showToast({ title: '保存成功', icon: 'none' })
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.emit('refresh', { showLoading: false })
      }
    }
  }
}